/**
 * Copyright © 2018
 * 
 */
package com.adds.lvds.controller.schdeuler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.model.dict.SysDicttype;
import com.adds.lvds.model.scheduler.JobDetailModel;
import com.adds.lvds.security.BaseController;
import com.adds.lvds.service.impl.dict.DictInfoService;
import com.adds.lvds.service.impl.scheduler.SchedulerService;

/**
 * @author: zhangjian
 * @date: 2018年6月26日 上午10:17:00
 * @desc TODO
 */
@Controller
@RequestMapping(value = "/scheduler/task/")
public class SchedulerController extends BaseController<JobDetailModel> {
	@Inject
	private SchedulerService schedulerService;
	@Inject
	private DictInfoService dictInfoService;

	/**
	 * 显示任务列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "index")
	public String index(Model model, @Param(value = "pk_scene_id") String pk_scene_id,
			@Param(value = "pk_dict_id") String pk_dict_id) {
		model.addAttribute("pk_scene_id", pk_scene_id);
		model.addAttribute("pk_dict_id", pk_dict_id);
		Map<String, Object> map = new HashMap<>();
		map.put("parent_dictid", 54);
		List<SysDicttype> list = dictInfoService.findSysDicttypeByMap(map);
		model.addAttribute("list", list);
		return "/monit/task/task_main";
	}

	/**
	 * 加载任务列表信息
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "loadDataGrid")
	public Map<String, Object> loadDataGrid(@Param(value = "page") String page, @Param(value = "rows") String rows,
			@Param(value = "sidx") String sidx, @Param(value = "sord") String sord,
			@Param(value = "jobDetailModel") JobDetailModel jobDetailModel) {
		Map<String, Object> map = new HashMap<String, Object>();
		pageView = schedulerService.pageQrtzTriggersByCondition(getPageView(page, rows, sidx, sord), jobDetailModel);
		map.put("rows", pageView.getRecords());
		map.put("page", pageView.getPageNow());
		map.put("total", pageView.getPageCount());
		return map;
	}

	/**
	 * 发布任务处理方法
	 * 
	 * @param jobDetailModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "addJob")
	public Map<String, Object> addJob(JobDetailModel jobDetailModel) {
		String errmsg = "";
		String errcode = schedulerService.addJob(jobDetailModel);
		Map<String, Object> map = new HashMap<>();
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "监控任务配置数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "监控任务配置数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 初始化编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "editJob")
	public String editJob(Model model, @Param(value = "pk_task_id") String pk_task_id) {
		Map<String, Object> map = new HashMap<>();
		map.put("parent_dictid", 54);
		List<SysDicttype> list = dictInfoService.findSysDicttypeByMap(map);
		model.addAttribute("list", list);
		map.put("pk_task_id", pk_task_id);
		JobDetailModel jobDetailModel = schedulerService.getQrtzTriggersList(map);
		model.addAttribute("jobDetailModel", jobDetailModel);
		return "/monit/task/task_main_edit";
	}

	/**
	 * 发布任务处理方法
	 * 
	 * @param jobDetailModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "doEditJob")
	public Map<String, Object> doEditJob(JobDetailModel jobDetailModel) {
		String errmsg = "";
		String errcode = schedulerService.editJob(jobDetailModel);
		Map<String, Object> map = new HashMap<>();
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "监控任务配置数据编辑成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "监控任务配置数据编辑失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 启动任务
	 * 
	 * @param jobDetailModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "startJob")
	public Map<String, Object> startJob(@Param(value = "jobName") String jobName) {
		String errmsg = "";
		String errcode = schedulerService.startJobs(jobName);
		Map<String, Object> map = new HashMap<>();
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "[" + jobName + "] 任务启动成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "[" + jobName + "] 任务启动失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 暂停任务
	 * 
	 * @param jobDetailModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "pauseJob")
	public Map<String, Object> pauseJob(@Param(value = "jobName") String jobName) {
		String errmsg = "";
		String errcode = schedulerService.pauseJob(jobName);
		Map<String, Object> map = new HashMap<>();
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "[" + jobName + "] 任务暂停成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "[" + jobName + "] 任务暂停失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 批量删除任务
	 * 
	 * @param jobNameList
	 * @param jobGroupList
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "batchJob")
	public Map<String, Object> batchJob(
			@Param(value = "ids") String[] ids,
			@Param(value = "jobNames") String[] jobNames
			) {
		String errcode = schedulerService.delJob(ids,jobNames);
		Map<String, Object> map = new HashMap<>();
		String errmsg = "";
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "批量删除监控任务配置数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "批量删除监控任务配置数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}
}
